// 首先引入config，再引入模块header。由于，header依赖于jquery，index依赖与header，所以，index也中可以使用jQuery。
require(['./config'],()=>{
    require(['request','template','bootstrap','header','footer'],(request,template)=>{
        class List{
            constructor(){
                this.getList()
                this.click()
                this.dat = []
                this.flag = 1
                this.id = []
                this.list = []
            }    
            //获取数据并渲染
            getList(){ 
                console.log(1)               
                const id = location.search.slice(4)
                const val = decodeURIComponent(location.hash.slice(2))
                //以id 导航栏点击进入
                if(id){
                    request.getList(id).then(resp => {
                        if(resp.code === 200){
                            const { list } = resp.data.items
                            this.data = list
                            const { cname } = resp.data.category
                            const html = template('goodListTemplate',{list})
                            $('#goodList').html(html)
                            $('#activelist,#activeList,#ActiveList').html(cname)
                        }                    
                    })
                }else if(val){  //以搜索栏点击进入
                    $('#activelist,#activeList,#ActiveList').html(val)
                    //获取所有分类
                    request.getTabs().then(resp =>{
                        var arr3 = [] 
                        //获取所有分类下的商品                       
                        resp.data.list.forEach(item=>{
                            request.getList(item.id).then(resp=>{
                                this.list = resp.data.items.list.filter(shop=>{
                                    //如果是广告或者title中没有该字段就过滤掉
                                    return shop.type === 1 && shop.title.search(val) != -1  
                                })                        
                                if(this.list) {
                                    //拼接所有的对象，到数组arr3中
                                    arr3 = arr3.concat(this.list)
                                    this.data = arr3
                                    //数据渲染
                                    $('#goodList').html(template('goodListTemplate',{list:arr3}))  
                                }         
                            })                    
                        }) 
                    })
                }     
            }
            //点击筛选，判断是checkbox还是排序
            click(){                
                $('#form').on('click','input,li',e=>{
                    var target = e.target
                    this.dat = []
                    if(target.tagName === 'INPUT'){   
                        $('s').removeClass()  
                        this.priceSelect()
                    }else if(target.tagName === 'LI'){
                        if(this.flag === 1){
                            this.flag = 2
                            this.priceSelect(this.flag)
                        }else if(this.flag === 2){
                            this.flag = 1
                            this.priceSelect(this.flag)
                        }                        
                    }                                       
                })
            }
            priceSelect(flag){ 
                //初始化获取checkbox信息，整理成数组
                //价格数组
                var arr = [],arr1 =[]
                for(let i = 0; i < $('#form1 input').length;i++){
                    if($('#form1 input').eq(i).prop('checked')){
                        var text = $('#form1 input').eq(i).parent().text()
                        if(i == $('#form1 input').length-1){
                            arr.push([parseInt(text),1000000000])
                        }else{
                            arr.push(text.split('-'))
                        }                                              
                    }
                } 
                //有无券数组
                for(let i = 0; i < $('#form2 input').length;i++){
                    if($('#form2 input').eq(i).prop('checked')){
                        arr1.push(1)                                                                                        
                    }else{
                        arr1.push(0)
                    }
                } 
                //价格筛选   
                console.log(arr, arr1)             
                for(let i = 0;i < arr.length;i++){                    
                    this.dat.push(
                        ...this.data.filter(item =>{
                            return item.price > Number(arr[i][0]) && item.price <= Number(arr[i][1])
                       })
                    )                   
                }
                //有无券筛选
                if(arr1.toString() == [0,0].toString()){
                    this.dat =""
                }else if(arr1.toString() == [0,1].toString()){     //无券
                    this.dat = this.dat.filter(item =>{
                        return item.couponValue === ""
                    })  
                }else if(arr1.toString() == [1,0].toString()){     //有券
                    this.dat = this.dat.filter(item =>{                        
                        return item.couponValue != ""
                    }) 
                }
                //价格排序 根据是否有flag来判断是否需要做价格排序
                if(flag){
                    if(flag === 1){
                        function compare1(arg) {
                            return function(b, a) {
                                return b[arg] - a[arg];
                            }
                        }
                        $('s').removeClass().addClass('s1')
                        this.dat = this.dat.sort(compare1('price'));
                    }else if(flag === 2){
                        function compare2(arg) {
                            return function(a, b) {
                                return b[arg] - a[arg];
                            }
                        }
                        $('s').removeClass().addClass('s2')
                        this.dat = this.dat.sort(compare2('price'));
                    }
                }
                const html = template('goodListTemplate',{list:this.dat})
                $('#goodList').html(html)                
            }
        }
        new List()
    })
})